package service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import security.MySecurityContextHandle;
import service.interf.IIfwServerDate;

import common.impl.exception.ServiceException;

import dao.interf.IIfwServerDateDAO;
import dao.pojo.IfwServerDate;

public class IfwServerDateImpl extends BaseDataServiceImpl implements
		IIfwServerDate {

	private IIfwServerDateDAO ifwServerDateDao;

	public void setIfwServerDateDao(IIfwServerDateDAO ifwServerDateDao) {
		this.ifwServerDateDao = ifwServerDateDao;
	}

	public String get() throws Exception {
		List<IfwServerDate> ifwServerDateL = ifwServerDateDao.findAll(null);
		if (null == ifwServerDateL || ifwServerDateL.isEmpty()) {
			return "";
		} else {
			String curTime = ifwServerDateL.get(0).getCurTime();
			if (null != curTime && curTime.length() > 19)
				curTime = curTime.substring(0, 19);
			return curTime;
		}
	}

	public String edit(String curTime) throws Exception {
		// 验证
		String userName = MySecurityContextHandle.getUserName();
		if(!"admin".equals(userName))
			throw new ServiceException("请先登录");
		if (StringUtils.isEmpty(curTime))
			throw new ServiceException("时间不能为空");
		boolean isValid = myDateHandle
				.validDate(curTime, "yyyy-MM-dd HH:mm:ss");
		if (!isValid)
			throw new ServiceException("时间格式错误");
		ifwServerDateDao.deleteAll();
		IfwServerDate ifwServerDate = new IfwServerDate();
		ifwServerDate.setCurTime(curTime);
		ifwServerDateDao.save(ifwServerDate);
		return "1";
	}

}
